<?php

namespace App\Http\Controllers\Sale;

use App\Http\Controllers\Base\Base;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class Add extends Controller
{
    private $baseClass;

    function __construct()
    {
        $this->baseClass = new Base();
    }

    public function loadAdd(){
        $arrayData = array();
        if ($this->baseClass->checkLoginStatus() == true){
            $viewPage = 'add';
            $arrayData['menuOpen1'] = 'menu-open';
            $arrayData['menuOpen2'] = '';
            $arrayData['active1'] = 'active';
            $arrayData['active2'] = '';
        }
        else {
            $viewPage = 'login';
        }
        return view($viewPage, $arrayData);
    }

    public function loadAddJson(){
        $data = array();
        if ($this->baseClass->checkLoginStatus() == true){
            $id = $this->baseClass->htmlPurifier('id');
            $result = DB::table($this->baseClass->recordTable)
                ->where('id', '=', $id)
                ->get();
            if (count($result) > 0){
                $item = $result[0];

                $data = array(
                    'commit_time' => date('Y-m-d', $item->commit_time),
                    'send_time' => date('Y-m-d', $item->send_time),
                    'receiver' => $item->receiver,
                    'receiver_mobile' => $item->receiver_mobile,
                    'province' => $item->province,
                    'address' => $item->address,
                    'sender' => $item->sender,
                    'sender_mobile' => $item->sender_mobile,
                    'description' => $item->description,
                    'cherry_class' => $item->cherry_class,
                    'cherry_price' => $item->cherry_price,
                    'cherry_weight' => $item->cherry_weight,
                    'cherry_fahuo_weight' => $item->cherry_fahuo_weight,
                    'transport_fee' => $item->transport_fee,
                    'baozhuang_fee' => $item->baozhuang_fee,
                    'total' => $item->total,
                    'kuaidi_number' => $item->kuaidi_number,
                    'receive_time' => $item->receive_time > 0 ? date('Y-m-d', $item->receive_time) : ''
                );
            }
            $status = 0;
            $content = '获取成功';
        }
        else {
            $status = 101;
            $content = '尚未登录';
        }
        return $this->baseClass->returnJson($status, $content,$data);
    }

    public function add(){
        $data = array();
        if ($this->baseClass->checkLoginStatus() == true){
            $id = $this->baseClass->htmlPurifier('id');
            $commitTime = $this->baseClass->htmlPurifier('commitTime');
            $sendTime = $this->baseClass->htmlPurifier('sendTime');
            $receiver = $this->baseClass->htmlPurifier('receiver');
            $receiverMobile = $this->baseClass->htmlPurifier('receiverMobile');
            $province = $this->baseClass->htmlPurifier('province');
            $address = $this->baseClass->htmlPurifier('address');
            $sender = $this->baseClass->htmlPurifier('sender');
            $senderMobile = $this->baseClass->htmlPurifier('senderMobile');
            $description = $this->baseClass->htmlPurifier('description');
            $cherryClass = $this->baseClass->htmlPurifier('cherryClass');
            $cherryPrice = $this->baseClass->htmlPurifier('cherryPrice');
            $cherryWeight = $this->baseClass->htmlPurifier('cherryWeight');
            $faHuoWeight = $this->baseClass->htmlPurifier('faHuoWeight');
            $transportFee = $this->baseClass->htmlPurifier('transportFee');
            $baozhuangFee = $this->baseClass->htmlPurifier('baozhuangFee');
            $kuaiDiNumber = $this->baseClass->htmlPurifier('kuaiDiNumber');
            $total = $this->baseClass->htmlPurifier('total');
            $receiveTime = $this->baseClass->htmlPurifier('receiveTime');
            $insertArray = array(
                'commit_time' => strtotime($commitTime),
                'send_time' => strtotime($sendTime),
                'receiver' => $receiver,
                'receiver_mobile' => $receiverMobile,
                'province' => $province,
                'address' => $address,
                'sender' => $sender,
                'sender_mobile' => $senderMobile,
                'description' => $description,
                'cherry_class' => $cherryClass,
                'cherry_price' => $cherryPrice,
                'cherry_weight' => $cherryWeight,
                'cherry_fahuo_weight' => $faHuoWeight,
                'transport_fee' => $transportFee,
                'baozhuang_fee' => $baozhuangFee,
                'total' => $total,
                'kuaidi_number' => $kuaiDiNumber,
                'receive_time' => strtotime($receiveTime)
            );
            if ($id != ''){
                DB::table($this->baseClass->recordTable)
                    ->where('id', '=', $id)
                    ->update($insertArray);
                $content = '修改成功';
            }
            else {
                DB::table($this->baseClass->recordTable)
                    ->insert($insertArray);
                $content = '新增成功';
            }
            $status = 0;
        }
        else {
            $status = 101;
            $content = '尚未登录';
        }
        return $this->baseClass->returnJson($status, $content, $data);
    }
}
